package com.hyst.web;

import java.util.List;

import javax.servlet.http.HttpSession;

import com.hyst.domain.TeacherPeriodUsedCusmom;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hyst.common.Contants;
import com.hyst.common.ResultBean;
import com.hyst.common.ResultPage;
import com.hyst.domain.Admin;
import com.hyst.domain.PeriodEdits;
import com.hyst.domain.TeacherPeriod;
import com.hyst.domain.TeacherPeriodUsed;
import com.hyst.domain.TeacherSchedule;
import com.hyst.service.TeacherPeriodService;

/**
 * @Description:controller
 * @author：dy 
 *@Date：2018年08月25日
 */
@RestController
@RequestMapping("teacherPeriods")
@CrossOrigin(maxAge = 3600, origins = "*")
public class TeacherPeriodController {
	@Autowired
	private TeacherPeriodService teacherPeriodService;
	@Autowired
	private ObjectMapper objectMapper;
	
	@PostMapping("save/{teacherid}")
	public ResultBean<Integer> save(@RequestBody List<TeacherPeriod> list,@PathVariable("teacherid") String teacherId) {
		System.err.println(teacherId);
		System.err.println(list);
		int num=teacherPeriodService.save(teacherId,list);
		return ResultBean.success(num);
	}
	//批量更新状态
	@PostMapping("savePeriods")
	public ResultBean<Integer> savePeriods(@RequestBody PeriodEdits periodEdits,HttpSession session) {
		Admin admin=(Admin) session.getAttribute(Contants.SESSION_USER_INFO);
		periodEdits.setMsg(admin.getUname()+"保留");
		int num=teacherPeriodService.savePeriods(periodEdits);
		return ResultBean.success(num);
	}
//	@PostMapping("savePeriods")
//	public ResultBean<Integer> savePeriods(@RequestBody PeriodEdits periodEdits) {
//		System.out.println(periodEdits);
//		return ResultBean.success(0);
//	}
//	List<TeacherSchedule>
	@GetMapping("schedules")
	public ResultPage<TeacherSchedule> getTeacherSchedule(TeacherSchedule ts){
		return ResultPage.success(teacherPeriodService.getTeacherSchedules(ts));
	}
	@GetMapping("auditionTime/{studentid}")
	public ResultBean<String> getauditionTime(@PathVariable("studentid") String studentid, @RequestParam("teacherid") String teacherid){
		String time = teacherPeriodService.getAuditionTime(studentid,teacherid);
		time = StringUtils.isBlank(time) ? "" : time;
		return ResultBean.success(time);
	}


	// 2018-10-7 chenbin: 保存试听信息
	@PostMapping("periodUsed/save/{teacherid}")
	public ResultBean<String> savePeriodUsed(@RequestBody List<TeacherPeriodUsedCusmom> list, @PathVariable("teacherid") String teacherId) {
		System.err.println(teacherId);
		System.err.println(list);
		String num=teacherPeriodService.savePeriodUsed(teacherId,list);
		return ResultBean.success(num);
	}

	/**
	 * 正式课预留时间
	 * @param list
	 * @param teacherId
	 * @return
	 */
	@PostMapping("periodUsedFormal/save/{teacherid}")
	public ResultBean<String> savePeriodFormalUsed(@RequestBody List<TeacherPeriodUsedCusmom> list, @PathVariable("teacherid") String teacherId) {
		System.err.println(teacherId);
		System.err.println(list);
		String value=teacherPeriodService.savePeriodFormUsed(teacherId,list);
		return ResultBean.success(value);
	}
	
	
	
	@GetMapping("/new")
	public ResultPage<TeacherPeriod> getProjectsNew(TeacherPeriod teacherPeriod) {
		return ResultPage.success(teacherPeriodService.list(teacherPeriod));
	}

	@GetMapping
	public ResultBean<List<List<TeacherPeriod>>> getProjects(TeacherPeriod teacherPeriod) {
		return teacherPeriodService.teacherPeriods(teacherPeriod);
	}


	@GetMapping("used")
	public ResultBean<List<List<TeacherPeriod>>> getUsed(TeacherPeriod teacherPeriod) {
		return teacherPeriodService.teacherPeriods(teacherPeriod);
	}

	
	@GetMapping("getPeriodUsed")
	public ResultPage<TeacherPeriodUsed> getUsedProjects(TeacherPeriodUsed teacherPeriodUsed) {
		return ResultPage.success(teacherPeriodService.getPeriodUsedlist(teacherPeriodUsed));
	}

	@GetMapping("getPeriodUsedFormal")
	public ResultPage<TeacherPeriodUsed> getPeriodUsedFormal(TeacherPeriodUsed teacherPeriodUsed) {
		return ResultPage.success(teacherPeriodService.getPeriodUsedFormal(teacherPeriodUsed));
	}

	@PostMapping
	public ResultBean<TeacherPeriod> creat(final TeacherPeriod teacherPeriod) {
		return ResultBean.success(teacherPeriodService.insert(teacherPeriod));
	}

	/**
	 * @Description: 根据主键查询
	 * @param id
	 * @return ResultBean<TeacherPeriod>
	 */
	@GetMapping("{id}")
	public ResultBean<TeacherPeriod> getByPriKey(@PathVariable("id") final Integer id) {
		return ResultBean.success(teacherPeriodService.getByPriKey(id));
	}

	/**
	 * @Description: 根据主键更新全字段
	 * @param teacherPeriod
	 * @return ResultBean<TeacherPeriod>
	 */
	@PatchMapping("{id}")
	public ResultBean<TeacherPeriod> update(final TeacherPeriod teacherPeriod) {
		return ResultBean.success(teacherPeriodService.update(teacherPeriod));
	}

	/**
	 * @Description: 根据主键更新不为默认值的字段
	 * @param teacherPeriod
	 * @return ResultBean<TeacherPeriod>
	 */
	//@PutMapping("{id}")
	@PostMapping("id")
	public ResultBean<TeacherPeriod> updatePatch(@PathVariable("id") final Integer id,
			final TeacherPeriod teacherPeriod) {
		return ResultBean.success(teacherPeriodService.updateSelective(teacherPeriod));
	}

	/**
	 * @Description: 根据主键删除
	 * @param id
	 * @return ResultBean<TeacherPeriod>
	 */
	@DeleteMapping("{id}")
	public ResultBean<TeacherPeriod> delete(@PathVariable("id") final Integer id) {
		return ResultBean.success(teacherPeriodService.delete(id));
	}


}
